#!/bin/bash

# 项目名称
PROJECT_NAME="wsim"
# 镜像标签
IMAGE_TAG="0.0.1-SNAPSHOT"
# 容器名称
CONTAINER_NAME="wsim"
# 端口映射，根据你的项目实际端口修改
PORT_MAPPING="8060:8060"

# 构建 Docker 镜像
docker build -t "$PROJECT_NAME:$IMAGE_TAG" .
if [ $? -ne 0 ]; then
    echo "镜像构建失败，请检查 Dockerfile 或构建环境。"
    exit 1
fi

# 停止并删除旧的容器
if [ $(docker ps -aq -f name="$CONTAINER_NAME") ]; then
    docker stop "$CONTAINER_NAME"
    if [ $? -ne 0 ]; then
        echo "停止容器 $CONTAINER_NAME 失败。"
        exit 1
    fi
    docker rm "$CONTAINER_NAME"
    if [ $? -ne 0 ]; then
        echo "删除容器 $CONTAINER_NAME 失败。"
        exit 1
    fi
fi

# 运行新的容器
docker run -d -p "$PORT_MAPPING" --network my-network --name "$CONTAINER_NAME" "$PROJECT_NAME:$IMAGE_TAG"
if [ $? -ne 0 ]; then
    echo "容器启动失败，请检查镜像或容器配置。"
    exit 1
fi

echo "部署成功！$CONTAINER_NAME 容器已启动。"